package com.jinxin.platform.cdfaceacs.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jinxin.platform.cdfaceacs.common.CommonCodeEntity;
import com.jinxin.platform.cdfaceacs.common.ResultEntity;
import com.jinxin.platform.cdfaceacs.mapper.JurenAcsCaptureRecordMapper;
import com.jinxin.platform.cdfaceacs.pojo.domains.CaptureRecordParam;
import com.jinxin.platform.cdfaceacs.pojo.domains.JurenAcsCaptureRecord;
import com.jinxin.platform.cdfaceacs.pojo.entity.Constant;
import com.jinxin.platform.cdfaceacs.pojo.vo.DataResult2;
import com.jinxin.platform.cdfaceacs.redis.RedisUtil;
import com.jinxin.platform.cdfaceacs.service.JurenAcsCaptureRecordService;
import com.jinxin.platform.cdfaceacs.service.RestTemplateService;
import com.jinxin.platform.cdfaceacs.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2020-08-28
 * @Author: Lingnan
 **/
@Service
public class JurenAcsCaptureRecordServiceImpl implements JurenAcsCaptureRecordService {

    @Autowired
    private JurenAcsCaptureRecordMapper captureRecordMapper;
    @Autowired
    private RestTemplateService restTemplateService;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public int insert(JurenAcsCaptureRecord record) {
        return captureRecordMapper.insert(record);
    }

    @Override
    public int updateByKey(JurenAcsCaptureRecord record) {
        return captureRecordMapper.updateByKey(record);
    }

    @Override
    public JurenAcsCaptureRecord selectOne(HashMap param) {
        return captureRecordMapper.selectOne(param);
    }

    @Override
    public List<JurenAcsCaptureRecord> listPage(HashMap param) {
        return captureRecordMapper.listPage(param);
    }

    @Override
    public int listPageCount(HashMap param) {
        return captureRecordMapper.listPageCount(param);
    }

    @Override
    public ResultEntity updateData(String companyId, String startDate, String endDate) {
        int count = 0;
        CaptureRecordParam param = new CaptureRecordParam();
        param.setPage(1);
        int rows = 300;
        // 根据相差分钟数来定获取的条数 按照1分钟60次
        if(!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)){
            rows = (int) (DateUtil.dateDiffMin(startDate,endDate) * 60);
        }
        param.setRows(rows); //因为是20分钟更新一次，所以一次取500
        param.setStartDate(startDate);
        param.setEndDate(endDate);
        ResultEntity resultEntity = restTemplateService.listCaptureRecord(param,companyId);
        if(resultEntity != null && "000".equals(resultEntity.getResultCode())){
            DataResult2 dataResult = (DataResult2)resultEntity.getResultContent();
            JSONArray jsonArray = dataResult != null ? dataResult.getRows() : null;
            if(jsonArray != null && jsonArray.size() > 0){
                String key = Constant.MODULE_PREX + "latest_capture_time";
                HashMap param2 = new HashMap();
                param2.put("companyId",companyId);
                param2.put("startDate",startDate);
                param2.put("endDate",endDate);
                List<JurenAcsCaptureRecord> list = captureRecordMapper.list(param2);
                List<String> idList = new ArrayList<>();
                for (JurenAcsCaptureRecord record : list) {
                    idList.add(record.getId());
                }
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    JurenAcsCaptureRecord info = JSON.parseObject(JSON.toJSONString(jsonObject), JurenAcsCaptureRecord.class);
                    // 保存最新一条数据的时间作为下一次请求接口的开始时间
                    if(i == 0){
                        String latestTime = DateUtil.transTimeToString(info.getCaptureTime());
                        redisUtil.set(key, latestTime);
                    }
                    if(info != null){
                        JurenAcsCaptureRecord record = captureRecordMapper.selectByKey(info.getId());
                        info.setCompanyId(companyId);
                        if(record == null){
                            captureRecordMapper.insert(info);
                            count++;
                        }else {
                            captureRecordMapper.updateByKey(info);
                            idList.remove(info.getId());
                        }

                    }
                }
                // 删除不存在数据
                for (String id : idList) {
                    captureRecordMapper.deleteByKey(id);
                }
            }
            return new ResultEntity(CommonCodeEntity.SUC_CODE,"成功同步数据"+count+"条","");
        }
        return new ResultEntity(CommonCodeEntity.FAIL,"数据更新失败","" );
    }
}
